package encheres.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import encheres.dao.HibernateUtil;
import encheres.dao.ICategorieDAO;
import encheres.dao.entities.Categorie;
import encheres.dao.entities.Client;
import encheres.dao.entities.Vente;

/**
 * Implémente les méthode DAO des catégories.
 * @author Nicolas
 *
 */
public class CategorieDAOImpl implements ICategorieDAO {

	/**
	 * {@inheritDoc}
	 */
	public List<Categorie> listCategories() throws HibernateException {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			String requete = "From Categorie";
			return (List<Categorie>)session.createQuery(requete).list();
			
		} catch (HibernateException e) {
			if(tx != null){
				tx.rollback();
			}
			throw e;
		} finally {
			session.close();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Categorie findCategorie(int idCategorie) throws HibernateException {
		Session session = null;
    	Transaction tx = null;
    	try {
    		session = HibernateUtil.getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		String requete = "From Categorie c where c.idCategorie="+idCategorie;
    		Categorie categorie = (Categorie) session.createQuery(requete).uniqueResult();
    		
    		return categorie;
    	} catch (HibernateException e) {
    		if(tx != null){
                tx.rollback();
            }
            throw e;
		} finally {
			session.close();
		}
	}

}
